﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data.Sql;
using LR4.Models;

namespace LR4.DAO
{
    public class ContactInfoDAO : DAO
    {
        public List<ContactInfo> GetAllRecords()        // Получение: Получить список всех контактных данных
        {
            Connect();
            List<ContactInfo> ContactInfoList = new List<ContactInfo>();

             try
             {
                SqlCommand command = new SqlCommand("SELECT * FROM ContactInfo", Connection);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    ContactInfo contact = new ContactInfo();
                    contact.ID = Convert.ToInt32(reader["ID"]);
                    contact.Name = Convert.ToString(reader["Name"]);
                    contact.Phone = Convert.ToString(reader["Phone"]);
                    ContactInfoList.Add(contact);
                }
                reader.Close();
             }

             catch (Exception)
             {
             }

             finally
             {
                Disconnect();
             }
             return ContactInfoList;
        }


        



        public bool AddRecord(ContactInfo contactInfo)        // Добавление: добавление контактных данных
        {
            bool result = true;
            Connect();

             try
             {
                SqlCommand cmd = new SqlCommand(
                    "INSERT INTO ContactInfo (Name, Phone) " +
                    "VALUES (@Name, @Phone)", Connection);

                cmd.Parameters.Add(new SqlParameter("@Name", contactInfo.Name));
                cmd.Parameters.Add(new SqlParameter("@Phone", contactInfo.Phone));
                

                cmd.ExecuteNonQuery();
             }
             catch (Exception)
             {
                result = false;
             }
             finally
             {
                Disconnect();
             }
             return result;
        }


        public bool EditRecord(int id, ContactInfo contactInfo)    // Редактирование: редактирование 
        {                                                          // контактных данных
            bool result = true;
            Connect();

             try
             {
                SqlCommand cmd = new SqlCommand("UPDATE ContactInfo SET Name=@Name," +
                    "Phone=@Phone WHERE (ID=" + id + ")", Connection);
                cmd.Parameters.AddWithValue("@Name", contactInfo.Name);
                cmd.Parameters.AddWithValue("@Phone", contactInfo.Phone);
               
                cmd.ExecuteNonQuery();
             }
             catch (Exception ex)
             {
                result = false;
             }
             finally
             {
                Disconnect();
             }
             return result;
        }

        
        
        public bool DeleteRecord(int ID)               // Удаление контактного лица
        {
            bool result = true;
            Connect();
             try
             {
                SqlCommand cmd = new SqlCommand(
                    "DELETE FROM ContactInfo WHERE ID=" + ID, Connection);

                cmd.ExecuteNonQuery();
             }
             catch (Exception)
             {
                result = false;
             }
             finally
             {
                Disconnect();
             }
             return result;
        }
        

    }

   }

